﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BaiDuSitemap.Setting
{
    public partial class Option : Form
    {
        public Option()
        {
            InitializeComponent();
        }

        private void Option_Shown(object sender, EventArgs e)
        {
            InitForm();
        }

        #region Event

        private void btnSave_Click(object sender, EventArgs e)
        {
            if(!CheckResult(DBO.Setting.Update("XMLns",this.tbXmlns.Text.Trim()),this.tbXmlns.Text)) return;

            if (!CheckResult(DBO.Setting.Update("URI", this.tbURI.Text.Trim()), this.lblURI.Text)) return;
            if (!CheckResult(DBO.Setting.Update("Regex", this.tbRegex.Text.Trim()), this.lblRegex.Text)) return;
            if (!CheckResult(DBO.Setting.Update("InterVal", this.nudInterVal.Value.ToString()), this.lblInterVal.Text)) return;

            if (!CheckResult(DBO.Setting.Update("DefaultPage", this.tbDefaultPages.Text.Trim()), this.lblDefaultPages.Text)) return;

            this.Close();
        }

        #endregion

        #region Method

        private bool CheckResult(int result,string name)
        {
            if (result == 1) return true;

            MessageBox.Show(string.Format("{0}保存失败，错误代码：{1}", name, result));
            return false;
        }

        private void InitForm()
        {
            this.tbXmlns.Text = DBO.Setting.Get("XMLns");
            this.tbURI.Text = DBO.Setting.Get("URI");
            this.tbRegex.Text = DBO.Setting.Get("Regex");
            this.nudInterVal.Value = decimal.Parse(DBO.Setting.Get("InterVal"));
            this.tbDefaultPages.Text = DBO.Setting.Get("DefaultPage");
        }

        #endregion
    }
}
